﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class YiGongSig : System.Web.UI.PageEx
{
    private DataSet ds = null;
    public static int iSelectId = 0;
    private string strResult = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.QueryString["Id"]), out iSelectId);
        if (!Page.IsPostBack)
        {
            DataBinds();
        }
    }
    private void DataBinds()
    {
        ds = DalEx.DbCompInfoEx.Select(" and cStatus=1");
        rptType.DataSource = ds;
        rptType.DataBind();

        Model.DbArticles M_DbArticles = Dal.DbArticlesDal.Select(iSelectId);
        if (M_DbArticles != null && (M_DbArticles.CEndDate.CompareTo(DateTime.Now)>=0 || M_DbArticles.CEndDate.CompareTo(DateTime.Now) == 0))
        {
            lblShowTitle.Text = M_DbArticles.ATitle;
            labActivityName.Text = M_DbArticles.ATitle;
            labActivityDate.Text = M_DbArticles.AAddTime.ToString("yyyy-MM-dd");
            labActivityOrg.Text = M_DbArticles.AAllClassIds;
            labLimitNums.Text = M_DbArticles.AHits.ToString();
            setMeta(M_DbArticles.AHtmlTitle, M_DbArticles.AKeywords, M_DbArticles.ADescription);
        }
        else
        {
            MessageBox("抱歉，当前报名信息不存在或已结束报名！","Default.aspx");
            return;
        }
        #region 如会员已登录则绑定数据
        if (DalEx.DbUserDalEx.CheckUserLogin(UserType, UserId, ref strResult))
        {
            pnlLoginInfo.Enabled = false;
            pnlLoginInfo.Visible = false;

            Model.DbUsers dbUsers = DalEx.DbUserDalEx.Select(UserId);
            if (dbUsers != null)
            {
                txtParentName.Text = dbUsers.FirstName + dbUsers.LastName;
                txtMobile.Text = dbUsers.Tel;
                hfUserId.Value = dbUsers.UserID.ToString();
            }
        }
        #endregion
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Model.DbArticles M_DbArticles = Dal.DbArticlesDal.Select(iSelectId);
        if (M_DbArticles == null || (M_DbArticles.CEndDate.CompareTo(DateTime.Now) < 0 && M_DbArticles.CEndDate.CompareTo(SqlHelper.MinDate) != 0))
        {
            MessageBox("抱歉，当前报名信息不存在或已结束报名！", "Default.aspx");
            return;
        }
        string strOrderSN = WebBase.Comm.GetOrderNo("ZC");
        string strActivityName = SqlHelper.String(labActivityName.Text);
        string strActivityDate = SqlHelper.String(labActivityDate.Text);
        string strActivityOrg = SqlHelper.String(labActivityOrg.Text);
        int intLimitNums = 0;
        int.TryParse(labLimitNums.Text, out intLimitNums);
        string strBabyClass = SqlHelper.String(txtBabyClass.Text);
        string strBabyName = SqlHelper.String(txtBabyName.Text);
        string strParentName = SqlHelper.String(txtParentName.Text);
        string strMobile = SqlHelper.String(txtMobile.Text);
        if (strBabyClass.Length > 50)
        {
            MessageBox("幼儿班级不能超过50个字！");
            return;
        }
        if (strBabyName.Length > 50)
        {
            MessageBox("幼儿姓名不能超过50个字！");
            return;
        }
        if (strParentName.Length > 100)
        {
            MessageBox("幼儿姓名不能超过100个字！");
            return;
        }
        if (strMobile.Length > 50)
        {
            MessageBox("联系电话不能超过50个字！");
            return;
        }

        int iUserId = -1;
        int.TryParse(hfUserId.Value, out iUserId);
        iUserId = iUserId == 0 ? -1 : iUserId;

        string strLoginName = SqlHelper.String(txtLoginUser.Text);
        string strEmail = SqlHelper.String(txtEmail.Text);
        string strLoginPwd = SqlHelper.String(txtLoginPwd.Text);

        if (iUserId < 1)
        {
            Model.DbUsers dbUsers = DalEx.DbUserDalEx.GetModel(" and UserName='" + strLoginName + "' and UserPwd='" + SqlHelper.MD5(strLoginPwd, 32) + "'");
            if (dbUsers != null)
            {
                iUserId = dbUsers.UserID;
                strLoginName = dbUsers.UserName;
            }
            else
            {
                dbUsers = new Model.DbUsers();
                dbUsers.UserName = strLoginName;
                dbUsers.Email = strEmail;
                dbUsers.UserPwd = strLoginPwd;
                dbUsers.Sex = 1;
                dbUsers.AddTime = DateTime.Now;
                dbUsers.LastLoginTime = SqlHelper.MinDate;
                dbUsers.TotalCost = 0;
                dbUsers.LoginTimes = 0;
                dbUsers.Status = 1;
                dbUsers.LastIP = "127.0.0.1";
                if (DalEx.DbUserDalEx.Insert(dbUsers, ref strResult, ref iUserId) < 1)
                {
                    MessageBox(strResult);
                    return;
                }
            }
        }

        Model.BaoMing3 M_BaoMing = new Model.BaoMing3();
        M_BaoMing.OrderNO = strOrderSN;
        M_BaoMing.ActivityDate = Convert.ToDateTime(strActivityDate);
        M_BaoMing.ActivityID = iSelectId;
        M_BaoMing.ActivityName = strActivityName;
        M_BaoMing.ActivityOrg = strActivityOrg;
        M_BaoMing.LimitNums = intLimitNums;
        M_BaoMing.BabyClass = strBabyClass;
        M_BaoMing.BabyName = strBabyName;
        M_BaoMing.ParentName = strParentName;
        M_BaoMing.Mobile = strMobile;
        M_BaoMing.Status = 0;
        M_BaoMing.Notes = "";
        M_BaoMing.Checkinfos = "";
        M_BaoMing.UserId = iUserId;
        M_BaoMing.AddTime = DateTime.Now;

        int iResult = 0;
        iResult = DalEx.BaoMing3Ex.Insert(M_BaoMing, ref strResult);
        if (iResult > 0)
        {
            UserType = 1;
            UserId = iUserId;
            UserName = strLoginName;
            MessageBox(strResult, "MyHomeEventBaoMing.aspx");
        }
        else
        {
            MessageBox(strResult);
        }
    }
    private void setMeta(string title, string key, string des)
    {
        string strWebTitle = string.Empty, strWebKeywords = string.Empty, strWebDescription = string.Empty;
        Model.DbSystem dbSystem = Dal.DbSystemDal.Select(1);
        if (dbSystem != null)
        {
            strWebTitle = dbSystem.STitle;
            strWebKeywords = dbSystem.SKeywords;
            strWebDescription = dbSystem.SDescription;
        }
        HtmlMeta keywords = new HtmlMeta(); // keywords
        HtmlMeta description = new HtmlMeta(); // description
        keywords.Name = "keywords";
        keywords.Content = string.IsNullOrEmpty(key) ? strWebKeywords : key;

        this.Title = string.IsNullOrEmpty(title) ? strWebTitle : title;

        description.Name = "description";
        description.Content = string.IsNullOrEmpty(des) ? strWebDescription : des;

        Page.Header.Controls.Add(keywords);
        Page.Header.Controls.Add(description);
    }
}
